﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace States
{
    public class MultiplayerSelectionState : BaseState
    {
        public enum ControllerStatus { Ready = 0, NotReady, Disconnected }
        private static MultiplayerSelectionState theMultiplayerSelectionState;
        Texture2D MultiplayerBackground;
        Texture2D ReadyImage, NotReadyImage, DisconnectedImage;

        Texture2D P1StatusImage, P2StatusImage, P3StatusImage, P4StatusImage;
        Vector2 P1StatusPosition, P2StatusPosition, P3StatusPosition, P4StatusPosition;
        ControllerStatus P1Status, P2Status, P3Status, P4Status;
        GamePadState Player1Pad, Player2Pad, Player3Pad, Player4Pad;


        private MultiplayerSelectionState()
        {
            theSpriteBatch = RoyalRumble.MainGame.spriteBatch;
            theContentManager = RoyalRumble.MainGame.theContent;
            P1Status = P2Status = P3Status = P4Status = ControllerStatus.Disconnected;
            P1StatusPosition = new Vector2(80, 300);
            P2StatusPosition = new Vector2(1160, 300);
            P3StatusPosition = new Vector2(80, 860);
            P4StatusPosition = new Vector2(1160, 860);
        }

        public static MultiplayerSelectionState Instance
        {
            get
            {
                if (theMultiplayerSelectionState == null)
                    theMultiplayerSelectionState = new MultiplayerSelectionState();
                return theMultiplayerSelectionState;
            }
        }

        public override void Enter()
        {
            theSpriteBatch = RoyalRumble.MainGame.spriteBatch;
            theContentManager = RoyalRumble.MainGame.theContent;
            LoadGameTextures();
        }
        public void LoadGameTextures()
        {
            MultiplayerBackground = theContentManager.Load<Texture2D>("MultiplayerScreen/MPBG");
            ReadyImage = theContentManager.Load<Texture2D>("MultiplayerScreen/ReadyImage");
            NotReadyImage = theContentManager.Load<Texture2D>("MultiplayerScreen/NotReadyImage");
            DisconnectedImage = theContentManager.Load<Texture2D>("MultiplayerScreen/DisconnectedImage");

            P1StatusImage = P2StatusImage = P3StatusImage = P4StatusImage = NotReadyImage;
        }
        public override void Exit()
        {

        }

        public override void GetInput(float ElapsedTime)
        {
            //  Player 1
            if (GamePad.GetState(PlayerIndex.One).IsConnected)
            {
                Player1Pad = GamePad.GetState(PlayerIndex.One);
                if (Player1Pad.Buttons.A == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player1.PlayerActive = true;
                    P1Status = ControllerStatus.Ready;
                    P1StatusImage = ReadyImage;
                }
                if (Player1Pad.Buttons.B == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player1.PlayerActive = false;
                    P1Status = ControllerStatus.NotReady;
                    P1StatusImage = NotReadyImage;
                }

                if (States.GameplayState.Instance.GameSession.Player1.PlayerActive == true)
                {
                    if (Player1Pad.Buttons.Start == ButtonState.Pressed)
                    {
                        RoyalRumble.MainGame.ChangeState(States.GameplayState.Instance);
                    }
                }
            }
            else
            {
                P1Status = ControllerStatus.Disconnected;
                P1StatusImage = DisconnectedImage;
            }

            //  Player 2
            if (GamePad.GetState(PlayerIndex.Two).IsConnected)
            {
                Player2Pad = GamePad.GetState(PlayerIndex.Two);
                if (Player2Pad.Buttons.A == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player2.PlayerActive = true;
                    P2Status = ControllerStatus.Ready;
                    P2StatusImage = ReadyImage;
                }
                if (Player2Pad.Buttons.B == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player2.PlayerActive = false;
                    P2Status = ControllerStatus.NotReady;
                    P2StatusImage = NotReadyImage;
                }

                if (States.GameplayState.Instance.GameSession.Player2.PlayerActive == true)
                {
                    if (Player2Pad.Buttons.Start == ButtonState.Pressed)
                    {
                        RoyalRumble.MainGame.ChangeState(States.GameplayState.Instance);
                    }
                }
            }
            else
            {
                P2Status = ControllerStatus.Disconnected;
                P2StatusImage = DisconnectedImage;
            }

            //  Player 3
            if (GamePad.GetState(PlayerIndex.Three).IsConnected)
            {
                Player3Pad = GamePad.GetState(PlayerIndex.Three);
                if (Player3Pad.Buttons.A == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player3.PlayerActive = true;
                    P3Status = ControllerStatus.Ready;
                    P3StatusImage = ReadyImage;
                }
                if (Player3Pad.Buttons.B == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player3.PlayerActive = false;
                    P3Status = ControllerStatus.NotReady;
                    P3StatusImage = NotReadyImage;
                }

                if (States.GameplayState.Instance.GameSession.Player3.PlayerActive == true)
                {
                    if (Player3Pad.Buttons.Start == ButtonState.Pressed)
                    {
                        RoyalRumble.MainGame.ChangeState(States.GameplayState.Instance);
                    }
                }
            }
            else
            {
                P3Status = ControllerStatus.Disconnected;
                P3StatusImage = DisconnectedImage;
            }

            //  Player 4
            if (GamePad.GetState(PlayerIndex.Four).IsConnected)
            {
                Player4Pad = GamePad.GetState(PlayerIndex.Four);
                if (Player4Pad.Buttons.A == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player4.PlayerActive = true;
                    P4Status = ControllerStatus.Ready;
                    P4StatusImage = ReadyImage;
                }
                if (Player4Pad.Buttons.B == ButtonState.Pressed)
                {
                    States.GameplayState.Instance.GameSession.Player4.PlayerActive = false;
                    P4Status = ControllerStatus.NotReady;
                    P4StatusImage = NotReadyImage;
                }

                if (States.GameplayState.Instance.GameSession.Player4.PlayerActive == true)
                {
                    if (Player4Pad.Buttons.Start == ButtonState.Pressed)
                    {
                        RoyalRumble.MainGame.ChangeState(States.GameplayState.Instance);
                    }
                }
            }
            else
            {
                P4Status = ControllerStatus.Disconnected;
                P4StatusImage = DisconnectedImage;
            }
        }

        public override void Update(float ElapsedTime)
        {
            GetInput(ElapsedTime);
        }
        public override void Render(float ElapsedTime)
        {
            theDevice.Clear(Color.White);
            theSpriteBatch.Begin();
            theSpriteBatch.Draw(MultiplayerBackground, new Vector2(0, 0), Color.White);

            theSpriteBatch.Draw(P1StatusImage, P1StatusPosition, Color.White);
            theSpriteBatch.Draw(P2StatusImage, P2StatusPosition, Color.White);
            theSpriteBatch.Draw(P3StatusImage, P3StatusPosition, Color.White);
            theSpriteBatch.Draw(P4StatusImage, P4StatusPosition, Color.White);

            theSpriteBatch.End();
        }
    }
}